package com.fruits.controller;


import com.fruits.domain.Cart;
import com.fruits.domain.Fruit;
import com.fruits.domain.User;
import com.fruits.service.CartService;
import com.fruits.service.impl.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/cartServlet")
public class CartServlet extends BaseServlet {
    private CartService cartService = new CartServiceImpl();
  // 添加购物车或者关注列表
  public String add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
   //获取参数
      String fidStr = request.getParameter("fid");
      String uidStr = request.getParameter("uid");
      String str = request.getParameter("str");
        int uid = Integer.parseInt(uidStr);
        int fid = Integer.parseInt(fidStr);
      //判断是否登录
      if(uid!=0){
          //添加到购物车
          Cart cart = cartService.find(uid, fid);
          if(cart==null){
              cart=new Cart();
              cart.setUid(uid);
              cart.setFid(fid);
              if("cart".equals(str)) {
                  cart.setIsCart(true);
                  cart.setIsStar(false);
              } else if("star".equals( str)){
                  cart.setIsStar(true);
                  cart.setIsCart(false);
              }
              cartService.add(uid,cart);
          }
          else{//以及存在在购物车，那么我就关注

            if("cart".equals( str)){
                cart.setIsCart(true);
                cart.setIsStar( cart.isStar());
            } else if ("star".equals(str)) {
                cart.setIsStar(true);
                cart.setIsCart( cart.isCart());


            }
            //更新操作
              cartService.update(uid,cart);
          }
          return "forward:/fruitServlet?method=info&uid=" + uid + "&fid=" + fid;
      }else{
          request.setAttribute("showError", "true");
          return "forward:/login.jsp";
      }
  }
//显示我的购物车和我的关注列表
public String show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
    int uid=Integer.parseInt(req.getParameter("uid"));
    String boob=req.getParameter("boob");
    //
    boolean flag =false;
    if(boob.equals("cart")){
        flag=true;
    }
    if(uid!=0){
        List<Fruit> fruits = cartService.show(uid, flag);
        req.setAttribute("fruits",fruits);
        if(flag){
            return "forward:/showcart.jsp";
        }else{
            return "forward:/showstar.jsp";
        }
    }else{
        //跳转到登录页面,提示用户登录
        req.setAttribute("showError",true);
        return "forward:/login.jsp";
    }

}
public void  num(HttpServletRequest request, HttpServletResponse response) {
      //获取登陆用户
    HttpSession session = request.getSession();
   User user=(User)session.getAttribute("user");
   int num=0;
    if(user!=null){
        List<Cart> carts = cartService.findById(user.getId());
         num=carts.size();
        request.setAttribute("num",num);
    }
}
public String delete(HttpServletRequest request, HttpServletResponse response){
    //获取参数
    String fidStr = request.getParameter("fid");
    String uidStr = request.getParameter("uid");
    String str =  request.getParameter("str");
    int uid=Integer.parseInt(uidStr);
    int fid=Integer.parseInt(fidStr);
    Cart cart=new Cart();
    cart.setUid(uid);
    cart.setFid(fid);
  //调用 service
    if(uid!=0){
        //从数据库里面查询
        Cart returnCart = cartService.find(uid, fid);
        if("cart".equals( str)){
            cart.setIsCart(false);
            cart.setIsStar(returnCart.isStar());
        } else if ("star".equals(str)) {
            cart.setIsStar(false);
            cart.setIsCart(returnCart.isCart());

        }
        if(cart.isCart()||cart.isStar()){//假如有关注或者添加购物车，那么就更新
            cartService.update(uid, cart);

        } else {
            cartService.delete(uid, fid);
        }
    }
        if("cart".equals(str)){
            return "forward:/cartServlet?method=show&uid=" + uid + "&boob=cart";
        } else{
            return "forward:/cartServlet?method=show&uid=" + uid + "&boob=star";

        }

  }



}
